package com.itheima.bos.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itheima.bos.dao.system.UserRepository;
import com.itheima.bos.domain.system.Role;
import com.itheima.bos.domain.system.User;
import com.itheima.bos.service.system.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserRepository userRepository;
	
	public User findByUsername(String username){
		return userRepository.findByUsername(username);
	}

	@Override
	public List<User> findAll() {
		return userRepository.findAll();
	}

	@Override
	public void saveUser(User user, String[] roleIds) {
		//保存之后，user就会成为持久太对象，之后再操作时就不用再操作数据库
		userRepository.save(user);
		if (roleIds != null) {
			for (String roleId : roleIds) {
				Role role = new Role();
				role.setId(Integer.parseInt(roleId));
				user.getRoles().add(role);
			}
		}
	}
}
